package ru.spb.osll.myfit.client.widget.filter;


import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.*;

import ru.spb.osll.myfit.client.widget.WatermarkedTextBox;
import ru.spb.osll.myfit.common.filter.MatchFilter;
import ru.spb.osll.myfit.common.filter.PageResultFilter;
import static ru.spb.osll.myfit.client.widget.IStyleConstants.*;


/**
 * User: Alexander Opeykin
 * Date: 4/23/12
 */


/**
 * Filtration panel widget. Let user add modify and delete filters.
 */
public class FiltrationWidget extends Composite {
  private List<FilterWidget> m_widgets = new ArrayList<FilterWidget>();
  private VerticalPanel m_filtersPanel;
  Button m_filterButton = new Button();

  public FiltrationWidget() {
    DisclosurePanel filterDisclosurePanel = new DisclosurePanel("Filter");
    filterDisclosurePanel.setAnimationEnabled(true);
    initWidget(filterDisclosurePanel);

    VerticalPanel filterMainPanel = new VerticalPanel();
    filterDisclosurePanel.add(filterMainPanel);

    m_filtersPanel = new VerticalPanel();
    filterMainPanel.add(m_filtersPanel);

    final FilterAddWidget addWidget = new FilterAddWidget();
    filterMainPanel.add(addWidget);
    addWidget.addFilterCreateHandler(new ClickHandler() {
      @Override
      public void onClick(ClickEvent clickEvent) {
        if (!addWidget.getName().isEmpty()) {
          addFilter(new MatchFilter(addWidget.getName(), addWidget.getValue()));
          addWidget.clean();
        } else {
          Window.alert("No name specified for filter");
        }
      }
    });

    filterMainPanel.add(new HTML("<hr/>"));
    m_filterButton.setText("Apply");
    filterMainPanel.add(m_filterButton);
  }

  public void addFilters(List<PageResultFilter> filters) {
    for (PageResultFilter filter : filters) {
      addFilter(filter);
    }
  }

  public void addFilter(PageResultFilter filter) {
    FilterWidgetCreator widgetCreator = new FilterWidgetCreator();
    FilterWidget widget = widgetCreator.create(filter);
    widget.addDeleteHandler(new FilterDeleteHandler(widget));
    m_widgets.add(widget);
    m_filtersPanel.add(widget);
  }

  public void addFilterClickHandler(ClickHandler handler) {
    m_filterButton.addClickHandler(handler);
  }

  public List<PageResultFilter> getFilters() {
    List<PageResultFilter> filters = new LinkedList<PageResultFilter>();

    for (FilterWidget widget : m_widgets) {
      filters.add(widget.getFilter());
    }

    return filters;
  }

  private class FilterDeleteHandler implements ClickHandler {
    private final FilterWidget widget;

    private FilterDeleteHandler(FilterWidget widget) {
      this.widget = widget;
    }

    @Override
    public void onClick(ClickEvent clickEvent) {
      m_widgets.remove(widget);
      widget.removeFromParent();
    }
  }

  private class FilterAddWidget extends Composite {
    final WatermarkedTextBox nameTextBox = new WatermarkedTextBox();
    final WatermarkedTextBox valueTextBox = new WatermarkedTextBox();
    final Button addButton = new Button();

    public FilterAddWidget() {
      HorizontalPanel panel = new HorizontalPanel();
      initWidget(panel);
      panel.setStyleName(FILTER_PANEL_STYLE);

      nameTextBox.addStyleName(FILTER_NAME_STYLE);
      nameTextBox.setWatermarkStyle(GRAY_ITALIC_TEXT_STYLE);
      nameTextBox.setWatermark("Name");
      panel.add(nameTextBox);

      valueTextBox.addStyleName(FILTER_VALUE_STYLE);
      valueTextBox.setWatermarkStyle(GRAY_ITALIC_TEXT_STYLE);
      valueTextBox.setWatermark("Value");
      panel.add(valueTextBox);

      addButton.addStyleName(FILTER_ADD_BUTTON_STYLE);
      panel.add(addButton);
    }

    public String getName() {
      return nameTextBox.getText().trim();
    }

    public String getValue() {
      return valueTextBox.getText().trim();
    }

    public void clean() {
      nameTextBox.setText("");
      valueTextBox.setText("");
    }

    public void addFilterCreateHandler(ClickHandler handler) {
      addButton.addClickHandler(handler);
    }
  }
}
